package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FunctionDAO;
import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;

import cn.edu.scau.cmi.hombio.domain.Function;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Function entities
 * 
 */

@Service("FunctionService")

@Transactional
public class FunctionServiceImpl implements FunctionService {

	/**
	 * DAO injected by Spring that manages Function entities
	 * 
	 */
	@Autowired
	private FunctionDAO functionDAO;

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * Instantiates a new FunctionServiceImpl.
	 *
	 */
	public FunctionServiceImpl() {
	}

	/**
	 * Load an existing Function entity
	 * 
	 */
	@Transactional
	public Set<Function> loadFunctions() {
		return functionDAO.findAllFunctions();
	}

	/**
	 * Save an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public Function saveFunctionRoleAuthorities(Integer id, RoleAuthority related_roleauthorities) {
		Function function = functionDAO.findFunctionByPrimaryKey(id, -1, -1);
		RoleAuthority existingroleAuthorities = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingroleAuthorities != null) {
			existingroleAuthorities.setId(related_roleauthorities.getId());
			existingroleAuthorities.setAdd(related_roleauthorities.getAdd());
			existingroleAuthorities.setDelete(related_roleauthorities.getDelete());
			existingroleAuthorities.setCheck(related_roleauthorities.getCheck());
			existingroleAuthorities.setEdit(related_roleauthorities.getEdit());
			existingroleAuthorities.setMeno(related_roleauthorities.getMeno());
			related_roleauthorities = existingroleAuthorities;
		}

		related_roleauthorities.setFunction(function);
		function.getRoleAuthorities().add(related_roleauthorities);
		related_roleauthorities = roleAuthorityDAO.store(related_roleauthorities);
		roleAuthorityDAO.flush();

		function = functionDAO.store(function);
		functionDAO.flush();

		return function;
	}

	/**
	 * Return all Function entity
	 * 
	 */
	@Transactional
	public List<Function> findAllFunctions(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Function>(functionDAO.findAllFunctions(startResult, maxRows));
	}

	/**
	 * Return a count of all Function entity
	 * 
	 */
	@Transactional
	public Integer countFunctions() {
		return ((Long) functionDAO.createQuerySingleResult("select count(o) from Function o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Function entity
	 * 
	 */
	@Transactional
	public void saveFunction(Function function) {
		Function existingFunction = functionDAO.findFunctionByPrimaryKey(function.getId());

		if (existingFunction != null) {
			if (existingFunction != function) {
				existingFunction.setId(function.getId());
				existingFunction.setName(function.getName());
				existingFunction.setMeno(function.getMeno());
			}
			function = functionDAO.store(existingFunction);
		} else {
			function = functionDAO.store(function);
		}
		functionDAO.flush();
	}

	/**
	 * Delete an existing Function entity
	 * 
	 */
	@Transactional
	public void deleteFunction(Function function) {
		functionDAO.remove(function);
		functionDAO.flush();
	}

	/**
	 */
	@Transactional
	public Function findFunctionByPrimaryKey(Integer id) {
		return functionDAO.findFunctionByPrimaryKey(id);
	}

	/**
	 * Delete an existing RoleAuthority entity
	 * 
	 */
	@Transactional
	public Function deleteFunctionRoleAuthorities(Integer function_id, Integer related_roleauthorities_id) {
		RoleAuthority related_roleauthorities = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities_id, -1, -1);

		Function function = functionDAO.findFunctionByPrimaryKey(function_id, -1, -1);

		related_roleauthorities.setFunction(null);
		function.getRoleAuthorities().remove(related_roleauthorities);

		roleAuthorityDAO.remove(related_roleauthorities);
		roleAuthorityDAO.flush();

		return function;
	}
}
